package cn.cmcc.online.smsapi.ui.view;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.json.JSONObject;

import java.util.List;

import cn.cmcc.online.smsapi.ui.R;
import cn.cmcc.online.smsapi.ui.model.MetaData;
import cn.cmcc.online.smsapi.ui.model.MovieData;
import cn.cmcc.online.smsapi.ui.model.TableData;
import cn.cmcc.online.smsapi.ui.presenter.MoviePresenter;

public class MovieVistaImpl extends GeneralBaseVistaImpl implements MovieVista {
    private static final String TAG = "MovieVistaImpl";

    private TextView mTextViewMovieName;
    private TextView mTextViewTime;
    private TextView mExtraTableLine1Content;
    private TextView mExtraTableLine2Content;
    private TextView mExtraTableLine3Content;
    private TextView mExtraTableLine1Title;
    private TextView mExtraTableLine2Title;
    private TextView mExtraTableLine3Title;
    private RelativeLayout mExtraTableLine1Layout;
    private RelativeLayout mExtraTableLine2Layout;
    private RelativeLayout mExtraTableLine3Layout;
    private TextView mMainTableLine1Content;
    private TextView mMainTableLine2Content;
    private TextView mMainTableLine3Content;
    private TextView mMainTableLine1Title;
    private TextView mMainTableLine2Title;
    private TextView mMainTableLine3Title;
    private RelativeLayout mMainTableLine1Layout;
    private RelativeLayout mMainTableLine2Layout;
    private RelativeLayout mMainTableLine3Layout;
    private TextView mTextViewTicketTakingAdvice;
    private ImageView mMidSplitLine;

    private MoviePresenter mMoviePresenter = null;

    public MovieVistaImpl(Context context, MoviePresenter presenter) {
        super(context);

        mMoviePresenter = presenter;
        mMoviePresenter.setVista(this);

        initView();
    }

    // MUST put after parent class constructor
    private void initView() {
        // TODO
    }

    @Override
    protected void initLayout() {
        super.initLayout();
        super.addBody(View.inflate(mContext, R.layout.bubble_movie_body, null));

        mTextViewMovieName = findView(R.id.bubble_movie_number);
        mTextViewTime = findView(R.id.bubble_movie_time);
        mExtraTableLine1Content = findView(R.id.movie_extra_table_line1_content);
        mExtraTableLine2Content = findView(R.id.movie_extra_table_line2_content);
        mExtraTableLine3Content = findView(R.id.movie_extra_table_line3_content);

        mExtraTableLine1Title = findView(R.id.movie_extra_table_line1_title);
        mExtraTableLine2Title = findView(R.id.movie_extra_table_line2_title);
        mExtraTableLine3Title = findView(R.id.movie_extra_table_line3_title);

        mExtraTableLine1Layout = findView(R.id.movie_extra_table_line1_layout);
        mExtraTableLine2Layout = findView(R.id.movie_extra_table_line2_layout);
        mExtraTableLine3Layout = findView(R.id.movie_extra_table_line3_layout);

        mMainTableLine1Content = findView(R.id.movie_main_table_line1_content);
        mMainTableLine2Content = findView(R.id.movie_main_table_line2_content);
        mMainTableLine3Content = findView(R.id.movie_main_table_line3_content);

        mMainTableLine1Title = findView(R.id.movie_main_table_line1_title);
        mMainTableLine2Title = findView(R.id.movie_main_table_line2_title);
        mMainTableLine3Title = findView(R.id.movie_main_table_line3_title);

        mMainTableLine1Layout = findView(R.id.movie_main_table_line1_layout);
        mMainTableLine2Layout = findView(R.id.movie_main_table_line2_layout);
        mMainTableLine3Layout = findView(R.id.movie_main_table_line3_layout);

        mTextViewTicketTakingAdvice = findView(R.id.bubble_movie_ticketNote);

        mMidSplitLine = findView(R.id.bubble_movie_dotted_split_imm);

    }

    @Override
    public void bind(Context context, JSONObject data) {
        super.bind(context, data);
        mMoviePresenter.bind(data);
    }

    // MovieVista start
    @Override
    public void showBody(MovieData data, MetaData metaData) {
        if (data == null) {
            return;
        }

        if (metaData != null && metaData.getHighlightColor() != null && !"".equalsIgnoreCase(metaData.getHighlightColor())) {
            mTextViewTime.setTextColor(Color.parseColor(metaData.getHighlightColor()));
        }

        if (!TextUtils.isEmpty(data.getName())) {
            mTextViewMovieName.setText(data.getName());
            mTextViewMovieName.setVisibility(View.VISIBLE);
        }

        if (!TextUtils.isEmpty(data.getTime())) {
            mTextViewTime.setText(data.getTime());
            mTextViewTime.setVisibility(View.VISIBLE);
        }

        mExtraTableLine1Content.setText(data.getSeat());
        mExtraTableLine2Content.setText(data.getRoom());
        mExtraTableLine3Content.setText(data.getTheatre());
        mMainTableLine1Content.setText(data.getVerifyCode());
        mMainTableLine2Content.setText(data.getTicketCode());
        mTextViewTicketTakingAdvice.setText(data.getTicketNote());
        // TODO
    }

    @Override
    public void initDefaultView() {
        super.initDefaultView();

        mTextViewMovieName.setVisibility(View.GONE);
        mTextViewTime.setVisibility(View.GONE);

        mTextViewTicketTakingAdvice.setText(null);
        mTextViewTicketTakingAdvice.setVisibility(View.GONE);
        mTextViewTicketTakingAdvice.setVisibility(View.GONE);

        mExtraTableLine1Layout.setVisibility(View.GONE);
        mExtraTableLine2Layout.setVisibility(View.GONE);
        mExtraTableLine3Layout.setVisibility(View.GONE);

        mMainTableLine1Layout.setVisibility(View.GONE);
        mMainTableLine2Layout.setVisibility(View.GONE);
        mMainTableLine3Layout.setVisibility(View.GONE);

        mMidSplitLine.setVisibility(View.GONE);

    }

    @Override
    public void showSplltLine(int viewShowState) {
        mMidSplitLine.setVisibility(viewShowState);
    }

    @Override
    public void showTable(List<TableData> dataList) {
        if (dataList == null || dataList.size() == 0) {
            mMidSplitLine.setVisibility(View.GONE);
        }
        if (dataList != null) {
            if (dataList.size() >= 1) {
                TableData data1 = dataList.get(0);
                mExtraTableLine1Title.setText(data1.getKey());
                mExtraTableLine1Content.setText(data1.getValue());
                mExtraTableLine1Layout.setVisibility(View.VISIBLE);
            }
            if (dataList.size() >= 2) {
                TableData data2 = dataList.get(1);
                mExtraTableLine2Title.setText(data2.getKey());
                mExtraTableLine2Content.setText(data2.getValue());
                mExtraTableLine2Layout.setVisibility(View.VISIBLE);
            }
            if (dataList.size() >= 3) {
                TableData data3 = dataList.get(2);
                mExtraTableLine3Title.setText(data3.getKey());
                mExtraTableLine3Content.setText(data3.getValue());
                mExtraTableLine3Layout.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void showMainTable(List<TableData> dataList) {

        if (dataList == null || dataList.size() == 0) {
            mMidSplitLine.setVisibility(View.GONE);
        }

        if (dataList != null) {
            if (dataList.size() >= 1) {
                TableData data1 = dataList.get(0);
                mMainTableLine1Title.setText(data1.getKey());
                mMainTableLine1Content.setText(data1.getValue());
                mMainTableLine1Layout.setVisibility(View.VISIBLE);
            }
            if (dataList.size() >= 2) {
                TableData data2 = dataList.get(1);
                mMainTableLine2Title.setText(data2.getKey());
                mMainTableLine2Content.setText(data2.getValue());
                mMainTableLine2Layout.setVisibility(View.VISIBLE);
            }
            if (dataList.size() >= 3) {
                TableData data3 = dataList.get(2);
                mMainTableLine3Title.setText(data3.getKey());
                mMainTableLine3Content.setText(data3.getValue());
                mMainTableLine3Layout.setVisibility(View.VISIBLE);
            }
        }
    }


    @Override
    public void showNote(String note) {
        mTextViewTicketTakingAdvice.setText(note);
        mTextViewTicketTakingAdvice.setVisibility(View.VISIBLE);
    }

    // MovieVista end
}
